| Conditions | 1 |
| Paths | 4 |
| Total Lines | 68 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | define( |
||
| 3 | function ($, _) { |
||
| 4 | 'use strict'; |
||
| 5 | return { |
||
| 6 | init: function ($target, columns) { |
||
| 7 | |||
| 8 | var $headerRow = $target.find('thead tr'); |
||
| 9 | if($headerRow[0].innerHTML && $headerRow[0].innerHTML.length != 0){ |
||
|
|
|||
| 10 | return; |
||
| 11 | } |
||
| 12 | |||
| 13 | var $footerRow = $target.find('tfoot tr'); |
||
| 14 | var $tbody = $target.find('tbody'); |
||
| 15 | var values = $target.find('input.table-data').val(); |
||
| 16 | |||
| 17 | values = $.parseJSON(values?values:'{}'); |
||
| 18 | |||
| 19 | var emptyTitle = '<th class="AknGrid-headerCell" style="width: 47px"></th>'; |
||
| 20 | // Title for reorder column |
||
| 21 | $headerRow.append(emptyTitle); |
||
| 22 | $footerRow.append(emptyTitle); |
||
| 23 | _.each(columns, function (column) { |
||
| 24 | var th = "<th class='AknGrid-headerCell "+column.id+"'>"+column.text+"</th>"; |
||
| 25 | $headerRow.append(th); |
||
| 26 | $footerRow.append(th); |
||
| 27 | }.bind(this)); |
||
| 28 | // Title for delete button column |
||
| 29 | $headerRow.append(emptyTitle); |
||
| 30 | $footerRow.append(emptyTitle); |
||
| 31 | |||
| 32 | _.each(values, function(row) { |
||
| 33 | var htmlColumns = []; |
||
| 34 | _.each(columns, function (column) { |
||
| 35 | var value = ""; |
||
| 36 | if (column.id in row) { |
||
| 37 | value = row[column.id]; |
||
| 38 | } |
||
| 39 | |||
| 40 | htmlColumns.push(this.createColumn(column, value)); |
||
| 41 | }.bind(this)); |
||
| 42 | $tbody.append(this.createRow(htmlColumns)); |
||
| 43 | }.bind(this)); |
||
| 44 | }, |
||
| 45 | createColumn: function (column, value) { |
||
| 46 | var td = $("<td class='AknGrid-bodyCell "+column.id+"' data-code='"+column.id+"'>"+column.func.renderField({column: column, value: value})+"</td>"); |
||
| 47 | column.func.init(td, column, value); |
||
| 48 | |||
| 49 | return td; |
||
| 50 | }, |
||
| 51 | createRow: function (htmlColumns) { |
||
| 52 | var row = $('<tr class="flagbit-table-row AknGrid-bodyRow editable-item-row"></tr>'); |
||
| 53 | row.append($('<td class="AknGrid-bodyCell"><span class="handle"><i class="icon-reorder"></i></span></td>')); |
||
| 54 | _.each(htmlColumns, function (htmlColumn) { |
||
| 55 | row.append(htmlColumn); |
||
| 56 | }); |
||
| 57 | row.append($('<td class="AknGrid-bodyCell"><span class="btn btn-small AknButton AknButton--small AknIconButton--important delete-row"><i class="icon-trash"></i></span></td>')); |
||
| 58 | |||
| 59 | return row; |
||
| 60 | }, |
||
| 61 | createEmptyRow: function (columns) { |
||
| 62 | var htmlColumns = []; |
||
| 63 | _.each(columns, function (column) { |
||
| 64 | htmlColumns.push(this.createColumn(column, '')); |
||
| 65 | }.bind(this)); |
||
| 66 | |||
| 67 | return this.createRow(htmlColumns); |
||
| 68 | } |
||
| 69 | }; |
||
| 70 | } |
||
| 71 | ); |
||
| 72 |